package promotion

import (
	sqlc "bp/db/sqlc"
	"bp/internal/render"
	"bp/internal/service/entity/promotion"
	"bp/internal/util"
	promotionGrpc "bp/rpc/gen/webapi/promotion"
	"context"
)

// CreatePromotion 新建促销活动
func (s *PromotionWebApiImp) CreatePromotion(ctx context.Context, req *promotionGrpc.CreatePromotionReq) (res *promotionGrpc.CreatePromotionRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("promotion_imp/cmd.CreatePromotion=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &promotionGrpc.CreatePromotionRes{}

	var promotionPtr *sqlc.Promotion
	var conv PromotionPtrEntityToPbFunc
	err = s.dao.NewTxRunner().SqlcExecTx(ctx, func(q *sqlc.Queries) (err error) {
		createPromotionParamPtr, itemCategoryConfPtrs, itemConfPtrs, err := s.VldCreatePromotionReq(ctx, q, req)
		if err != nil {
			return
		}
		promotionPtr, err = s.entityHub.PromotionService.CreatePromotion(ctx, q, createPromotionParamPtr)
		if err != nil {
			return
		}
		err = s.entityHub.PromotionService.SetPromotionItemCategories(ctx, q, &promotion.SetPromotionItemCategoriesParam{
			PromotionPtr:         promotionPtr,
			ItemCategoryConfPtrs: itemCategoryConfPtrs,
		})
		if err != nil {
			return
		}
		err = s.entityHub.PromotionService.SetPromotionItems(ctx, q, &promotion.SetPromotionItemsParam{
			PromotionPtr: promotionPtr,
			ItemConfPtrs: itemConfPtrs,
		})
		if err != nil {
			return
		}
		_, err = s.entityHub.PromotionService.EnsurePromotionStat(ctx, q, false, &promotion.EnsurePromotionStatParam{
			PromotionPtr: promotionPtr,
		})
		if err != nil {
			return
		}
		conv = s.ConvPromotionPtrEntityToPb(itemCategoryConfPtrs, itemConfPtrs)
		slog.Infof("新建促销活动 ok")
		return
	})
	if err != nil {
		return
	}

	res.Promotion = conv(promotionPtr)
	return
}

// UpdatePromotion 更新促销活动
func (s *PromotionWebApiImp) UpdatePromotion(ctx context.Context, req *promotionGrpc.UpdatePromotionReq) (res *promotionGrpc.UpdatePromotionRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("promotion_imp/cmd.UpdatePromotion=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &promotionGrpc.UpdatePromotionRes{}

	var promotionPtr *sqlc.Promotion
	var conv PromotionPtrEntityToPbFunc
	err = s.dao.NewTxRunner().SqlcExecTx(ctx, func(q *sqlc.Queries) (err error) {
		updatePromotionParamPtr, itemCategoryConfPtrs, itemConfPtrs, err := s.VldUpdatePromotionReq(ctx, q, req)
		if err != nil {
			return
		}
		promotionPtr, err = s.entityHub.PromotionService.UpdatePromotion(ctx, q, updatePromotionParamPtr)
		if err != nil {
			return
		}
		err = s.entityHub.PromotionService.SetPromotionItemCategories(ctx, q, &promotion.SetPromotionItemCategoriesParam{
			PromotionPtr:         promotionPtr,
			ItemCategoryConfPtrs: itemCategoryConfPtrs,
		})
		if err != nil {
			return
		}
		err = s.entityHub.PromotionService.SetPromotionItems(ctx, q, &promotion.SetPromotionItemsParam{
			PromotionPtr: promotionPtr,
			ItemConfPtrs: itemConfPtrs,
		})
		if err != nil {
			return
		}
		conv = s.ConvPromotionPtrEntityToPb(itemCategoryConfPtrs, itemConfPtrs)
		slog.Infof("更新促销活动 ok")
		return
	})
	if err != nil {
		return
	}

	res.Promotion = conv(promotionPtr)
	return
}

// PublishPromotion 发布促销活动
func (s *PromotionWebApiImp) PublishPromotion(ctx context.Context, req *promotionGrpc.PublishPromotionReq) (res *promotionGrpc.PublishPromotionRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("promotion_imp/cmd.PublishPromotion=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &promotionGrpc.PublishPromotionRes{}

	var promotionPtr *sqlc.Promotion
	err = s.dao.NewTxRunner().SqlcExecTx(ctx, func(q *sqlc.Queries) (err error) {
		publishPromotionParamPtr, err := s.VldPublishPromotionReq(ctx, q, req)
		if err != nil {
			return
		}
		promotionPtr, err = s.entityHub.PromotionService.PublishPromotion(ctx, q, publishPromotionParamPtr)
		if err != nil {
			return
		}
		slog.Infof("发布促销活动 ok")
		return
	})
	if err != nil {
		return
	}

	res.PromotionStatus = promotionPtr.PromotionStatus
	return
}

// PausePromotion 暂停促销活动
func (s *PromotionWebApiImp) PausePromotion(ctx context.Context, req *promotionGrpc.PausePromotionReq) (res *promotionGrpc.PausePromotionRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("promotion_imp/cmd.PausePromotion=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &promotionGrpc.PausePromotionRes{}

	var promotionPtr *sqlc.Promotion
	err = s.dao.NewTxRunner().SqlcExecTx(ctx, func(q *sqlc.Queries) (err error) {
		pausePromotionParamPtr, err := s.VldPausePromotionReq(ctx, q, req)
		if err != nil {
			return
		}
		promotionPtr, err = s.entityHub.PromotionService.PausePromotion(ctx, q, pausePromotionParamPtr)
		if err != nil {
			return
		}
		slog.Infof("暂停促销活动 ok")
		return
	})
	if err != nil {
		return
	}

	res.PromotionStatus = promotionPtr.PromotionStatus
	return
}

// ResumePromotion 恢复促销活动
func (s *PromotionWebApiImp) ResumePromotion(ctx context.Context, req *promotionGrpc.ResumePromotionReq) (res *promotionGrpc.ResumePromotionRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("promotion_imp/cmd.ResumePromotion=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &promotionGrpc.ResumePromotionRes{}

	var promotionPtr *sqlc.Promotion
	err = s.dao.NewTxRunner().SqlcExecTx(ctx, func(q *sqlc.Queries) (err error) {
		resumePromotionParamPtr, err := s.VldResumePromotionReq(ctx, q, req)
		if err != nil {
			return
		}
		promotionPtr, err = s.entityHub.PromotionService.ResumePromotion(ctx, q, resumePromotionParamPtr)
		if err != nil {
			return
		}
		slog.Infof("恢复促销活动 ok")
		return
	})
	if err != nil {
		return
	}

	res.PromotionStatus = promotionPtr.PromotionStatus
	return
}
